package com.wy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wy.common.Result;
import com.wy.entity.*;
import com.wy.service.GoodsEvaluationService;
import com.wy.service.GoodsImageService;
import com.wy.service.GoodsInfoService;
import com.wy.service.UserInfoService;
import com.wy.util.EmailSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.ReactiveSubscription;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author:王渝
 * @Date: 2023/1/6  16:17
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/evaluation")
public class GoodsEvaluationController {

    @Autowired
    private GoodsEvaluationService evaluationService;

    @Autowired
    private GoodsInfoService goodsInfoService;

    @Value("${file.url}")
    private String url;

    @Autowired
    private GoodsImageService goodsImageService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private EmailSender emailSender;

    /**
     * 发布评论
     * @param goodsEvaluation 评论信息】
     * @return
     */
    @RequestMapping("/comment")
    public Result<String> comment(@RequestBody GoodsEvaluation goodsEvaluation){

        LambdaQueryWrapper<GoodsEvaluation> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(GoodsEvaluation::getGoodsId,goodsEvaluation.getGoodsId());
        lambdaQueryWrapper2.eq(GoodsEvaluation::getBuyerUserId,goodsEvaluation.getBuyerUserId());

        //查看是否评论过了
        int count = evaluationService.count(lambdaQueryWrapper2);
        if(count > 0){
            return Result.error("你已经评论过了");
        }

        LambdaQueryWrapper<GoodsInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsInfo::getGoodsId,goodsEvaluation.getGoodsId());
        lambdaQueryWrapper.select(GoodsInfo::getUserId);

        //查询卖家id
        //SELECT user_id FROM goods_info WHERE (goods_id = ?)
        GoodsInfo one = goodsInfoService.getOne(lambdaQueryWrapper);
        goodsEvaluation.setSellerUserId(one.getUserId());



        boolean b = evaluationService.saveComment(goodsEvaluation);
        if(b){
            return Result.success("评论成功");
        }
        return Result.error("评论失败");
    }

    /**
     * 根据卖方id查询全部评价
     * @param sellerUserId 卖方用户id
     * @return
     */
    @RequestMapping("/getAll/{sellerUserId}")
    public Result<List<GoodsEvaluation>> getAllBySellerUserId(@PathVariable Long sellerUserId) {
        //获取评价
        List<GoodsEvaluation> list = evaluationService.getAllBySellerUserId(sellerUserId);
        for (GoodsEvaluation goodsEvaluation : list) {
            //设置头像url
            goodsEvaluation.setHeadImage(url + goodsEvaluation.getHeadImage());
            //获取评价图片
            LambdaQueryWrapper<GoodsImage> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(GoodsImage::getEvaluationId,goodsEvaluation.getEvaluationId());
            List<GoodsImage> list1 = goodsImageService.list(queryWrapper);
            String []images = new String[list1.size()];
            int i = 0;
            for (GoodsImage goodsImage : list1) {
                //设置图片url
                images[i] = url + goodsImage.getImage();
                i++;
            }
            goodsEvaluation.setImages(images);
        }
        return Result.success(list);
    }

    /**
     * 分页查询评价
     * @param currentPage 当前页
     * @param pageSize 分页大小
     * @param status 1我的评价 2回复我的
     * @param userId 用户id
     * @return
     */
    @RequestMapping("/queryPage/{currentPage}/{pageSize}/{status}/{userId}")
    public Result<PageInfo<GoodsEvaluation>> queryPage(@PathVariable int currentPage,@PathVariable int pageSize,@PathVariable String status,@PathVariable Long  userId){
       PageInfo<GoodsEvaluation> pageInfo = null;

        if("1".equals(status)){
            pageInfo = evaluationService.getAllByBuyUserId(currentPage,pageSize,userId);
        }else {
            pageInfo = evaluationService.getAllByBuyUserId2(currentPage,pageSize,userId);
        }
        return Result.success(pageInfo);
    }

    /**
     * 查询评价详细数据
     * @param evaluationId
     * @return
     */
    @RequestMapping("/detail/{evaluationId}")
    public Result<GoodsEvaluation> queryDetail(@PathVariable Long evaluationId){

        GoodsEvaluation goodsEvaluation = evaluationService.getById(evaluationId);
        //获取评价图片
        LambdaQueryWrapper<GoodsImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsImage::getEvaluationId,goodsEvaluation.getEvaluationId());
        List<GoodsImage> list1 = goodsImageService.list(queryWrapper);
        String []images = new String[list1.size()];
        int i = 0;
        for (GoodsImage goodsImage : list1) {
            //设置图片url
            images[i] = url + goodsImage.getImage();
            i++;
        }
        goodsEvaluation.setImages(images);
        return Result.success(goodsEvaluation);
    }

    /**
     * 移除评论
     * @param evaluationId 评论id
     * @return
     */
    @RequestMapping("/remove/{evaluationId}")
    public Result<String> removeEvaluation(@PathVariable Long evaluationId){
        boolean b = evaluationService.removeById(evaluationId);
        if(b){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 分页查询评价
     * @param currentPage 当前页
     * @param pageSize 分页大小

     * @return
     */
    @RequestMapping("/queryPage/{currentPage}/{pageSize}")
    public Result<PageInfo<GoodsEvaluation>> queryPageAdmin(@PathVariable int currentPage,@PathVariable int pageSize){
        PageInfo<GoodsEvaluation> pageInfo = null;
        pageInfo = evaluationService.queryAllAdmin(currentPage,pageSize);
        return Result.success(pageInfo);
    }


    /**
     * 移除评论(管理员)
     * @param goodsEvaluation 评论
     * @return
     */
    @RequestMapping("/remove")
    public Result<String> removeEvaluationAdmin(@RequestBody GoodsEvaluation goodsEvaluation){
        boolean b = evaluationService.removeById(goodsEvaluation.getEvaluationId());
        if(b){
            LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(UserInfo::getUserId,goodsEvaluation.getBuyerUserId());
            lambdaQueryWrapper.select(UserInfo::getEmail);
            UserInfo one = userInfoService.getOne(lambdaQueryWrapper);
            emailSender.send(one.getEmail(),"你好!<br>你的的评论:\"" +
                    goodsEvaluation.getDetail() + "\"<br>因为违规，被管理员删除。");
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
